package GUI;
import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JRadioButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.SwingConstants;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.ImageIcon;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

public class AddGate extends JFrame
{
	public JList list; 
	public JPanel LeftPanel = new JPanel();
	public JPanel RightPanel = new JPanel();
	public boolean andf,notf,orf;
	ButtonGroup RadioButtons = new ButtonGroup();
	JRadioButton AndGate = new JRadioButton("And Gate");
	JRadioButton OrGate = new JRadioButton("Or Gate");
	JRadioButton NotGate = new JRadioButton("NotGate");
	JRadioButton XorGate = new JRadioButton("XorGate");
	JRadioButton XnorGate = new JRadioButton("XNorGate");
	JRadioButton NandGate = new JRadioButton("NandGate");
	JRadioButton NorGate = new JRadioButton("NorGate");
	public JButton And,Or,Not,Nor,Xor,Xnor,Nand;
	public Icon AndIcon,OrIcon,NotIcon,NorIcon,XorIcon,XnorIcon,NandIcon;
	AddGate()
	{
		JPanel AddGatePanel = new JPanel();		
        AddGatePanel.setLayout(new BorderLayout());
		
		JSplitPane AddGateSplitPane = new JSplitPane();
		add(AddGateSplitPane,BorderLayout.CENTER);
		
		JScrollPane LeftScrollPane = new JScrollPane();
		LeftScrollPane.setViewportBorder(null);
		AddGateSplitPane.setLeftComponent(LeftScrollPane);
		
		LeftScrollPane.setViewportView(LeftPanel);
		LeftPanel.setLayout(new BoxLayout(LeftPanel, BoxLayout.Y_AXIS));
		JScrollPane RightScrollPane = new JScrollPane();
		AddGateSplitPane.setRightComponent(RightScrollPane);
		RightScrollPane.setViewportView(RightPanel);		
		RightPanel.setLayout(new BorderLayout());
		
		RadioButtons.add(AndGate);		
		RadioButtons.add(OrGate);		
		RadioButtons.add(NotGate);		
		RadioButtons.add(XorGate);		
		RadioButtons.add(XnorGate);		
		RadioButtons.add(NandGate);		
		RadioButtons.add(NorGate);
		
		LeftPanel.add(AndGate);
		LeftPanel.add(OrGate);
		LeftPanel.add(NotGate);
		LeftPanel.add(XorGate);
		LeftPanel.add(XnorGate);
		LeftPanel.add(NandGate);
		LeftPanel.add(NorGate);
		
		AndGate.addItemListener(new ItemListener()
		{
			public void itemStateChanged(ItemEvent event) 
			{
				if(AndGate.isSelected())
				{
					AndIcon = new ImageIcon(getClass().getResource("Play.png"));
					RightPanel.add(And = new JButton("And",AndIcon),BorderLayout.CENTER);
					RightPanel.updateUI();
					And.addActionListener(new ActionListener()
					{
						public void actionPerformed(ActionEvent event) 
						{
							GUI.Components.AndGate obj=new GUI.Components.AndGate();
							obj.setAlwaysOnTop(true);							
							obj.setVisible(true);							
							dispose();
						}						
					});
				}
				else 
				{
					RightPanel.remove(And);						
				}
			}			
		}
		);
		
		OrGate.addItemListener(new ItemListener()
		{
			public void itemStateChanged(ItemEvent event) 
			{
				if(OrGate.isSelected())
				{
					OrIcon = new ImageIcon(getClass().getResource("Stop.png"));
					RightPanel.add(Or = new JButton("Or",OrIcon),BorderLayout.CENTER);
					RightPanel.updateUI();
					Or.addActionListener(new ActionListener()
					{
						public void actionPerformed(ActionEvent event) 
						{
							GUI.Components.OrGate obj=new GUI.Components.OrGate();
							obj.setAlwaysOnTop(true);							
							obj.setVisible(true);							
							dispose();
						}						
					});
				}
				else 
				{
					RightPanel.remove(Or);						
				}					
			}			
		}
		);
		
		NotGate.addItemListener(new ItemListener()
		{
			public void itemStateChanged(ItemEvent event) 
			{
				if(NotGate.isSelected())
				{
					NotIcon = new ImageIcon(getClass().getResource("Stop.png"));
					RightPanel.add(Not = new JButton("Not",NotIcon),BorderLayout.CENTER);
					RightPanel.updateUI();
					Not.addActionListener(new ActionListener()
					{
						public void actionPerformed(ActionEvent event) 
						{
							GUI.Components.NotGate obj=new GUI.Components.NotGate();
							obj.setAlwaysOnTop(true);							
							obj.setVisible(true);
							dispose();
						}						
					});
				}
				else 
				{
					RightPanel.remove(Not);						
				}
			}			
		}
		);
		
		XorGate.addItemListener(new ItemListener()
		{
			public void itemStateChanged(ItemEvent event) 
			{
				if(XorGate.isSelected())
				{
					XorIcon = new ImageIcon(getClass().getResource("Stop.png"));
					RightPanel.add(Xor = new JButton("Or",XorIcon),BorderLayout.CENTER);
					RightPanel.updateUI();
					Xor.addActionListener(new ActionListener()
					{
						public void actionPerformed(ActionEvent event) 
						{
							GUI.Components.OrGate obj=new GUI.Components.OrGate();
							obj.setAlwaysOnTop(true);							
							obj.setVisible(true);
							dispose();
						}						
					});
				}
				else 
				{
					RightPanel.remove(Xor);		
				}
			}			
		}
		);
		
		XnorGate.addItemListener(new ItemListener()
		{
			public void itemStateChanged(ItemEvent event) 
			{
				if(XnorGate.isSelected())
				{
					XnorIcon = new ImageIcon(getClass().getResource("Stop.png"));
					RightPanel.add(Xnor = new JButton("Or",XnorIcon),BorderLayout.CENTER);
					RightPanel.updateUI();
					Xnor.addActionListener(new ActionListener()
					{
						public void actionPerformed(ActionEvent event) 
						{
							GUI.Components.XNorGate obj=new GUI.Components.XNorGate();
							obj.setAlwaysOnTop(true);							
							obj.setVisible(true);
							dispose();
						}						
					});
				}
				else 
				{
					RightPanel.remove(Xnor);	
				}
							
			}			
		}
		);
		
		NandGate.addItemListener(new ItemListener()
		{
			public void itemStateChanged(ItemEvent event) 
			{
				if(NandGate.isSelected())
				{
					NandIcon = new ImageIcon(getClass().getResource("Stop.png"));
					RightPanel.add(Nand = new JButton("Or",NandIcon),BorderLayout.CENTER);
					RightPanel.updateUI();
					Nand.addActionListener(new ActionListener()
					{
						public void actionPerformed(ActionEvent event) 
						{
							GUI.Components.NandGate obj=new GUI.Components.NandGate();
							obj.setAlwaysOnTop(true);							
							obj.setVisible(true);
							dispose();
						}						
					});
				}
				else 
				{
					RightPanel.remove(Nand);		
				}
			}			
		}
		);
		
		NorGate.addItemListener(new ItemListener()
		{
			public void itemStateChanged(ItemEvent event) 
			{
				if(NorGate.isSelected())
				{
					NorIcon = new ImageIcon(getClass().getResource("Stop.png"));
					RightPanel.add(Nor = new JButton("Or",NorIcon),BorderLayout.CENTER);
					RightPanel.updateUI();
					Nor.addActionListener(new ActionListener()
					{
						public void actionPerformed(ActionEvent event) 
						{
							GUI.Components.OrGate obj=new GUI.Components.OrGate();
							obj.setAlwaysOnTop(true);							
							obj.setVisible(true);
							dispose();
						}						
					});
				}
				else 
				{
					RightPanel.remove(Nor);		
				}
			}			
		}		
		);
		
		/*AndGate obj=new AndGate();
		obj.setAlwaysOnTop(true);
		obj.setResizable(false);
		obj.setVisible(true);*/	
	}	
}

